package com.ruoyi.his.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.his.domain.TMedicalDrugsPurchase;
import com.ruoyi.his.domain.TMedicalDrugsStorage;
import com.ruoyi.his.domain.vo.TMedicalDrugsPurchaseVo;
import com.ruoyi.his.service.ITMedicalDrugsPurchaseService;
import com.ruoyi.his.service.ITMedicalDrugsStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 药品采购Controller
 * 
 * @author ruoyi
 * @date 2021-09-10
 */
@RestController
@RequestMapping("/system/purchase")
public class TMedicalDrugsPurchaseController extends BaseController
{
    @Autowired
    private ITMedicalDrugsPurchaseService tMedicalDrugsPurchaseService;
    @Autowired
    private ITMedicalDrugsStorageService tMedicalDrugsStorageService;

    /**
     * 查询药品采购列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMedicalDrugsPurchaseVo tMedicalDrugsPurchaseVo)
    {
        startPage();
        List<TMedicalDrugsPurchaseVo> list = tMedicalDrugsPurchaseService.selectTMedicalDrugsPurchaseList(tMedicalDrugsPurchaseVo);
        return getDataTable(list);
    }

    /**
     * 导出药品采购列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:export')")
    @Log(title = "药品采购", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMedicalDrugsPurchaseVo tMedicalDrugsPurchaseVo)
    {
        List<TMedicalDrugsPurchaseVo> list = tMedicalDrugsPurchaseService.selectTMedicalDrugsPurchaseList(tMedicalDrugsPurchaseVo);
        ExcelUtil<TMedicalDrugsPurchaseVo> util = new ExcelUtil<TMedicalDrugsPurchaseVo>(TMedicalDrugsPurchaseVo.class);
        return util.exportExcel(list, "药品采购数据");
    }

    /**
     * 获取药品采购详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:query')")
    @GetMapping(value = "/{purchaseid}")
    public AjaxResult getInfo(@PathVariable("purchaseid") Long purchaseid)
    {
        return AjaxResult.success(tMedicalDrugsPurchaseService.selectTMedicalDrugsPurchaseByPurchaseid(purchaseid));
    }

    /**
     * 新增药品采购
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:add')")
    @Log(title = "药品采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMedicalDrugsPurchase tMedicalDrugsPurchase)
    {

        String time = new Date().getTime()+"";
        tMedicalDrugsPurchase.setBatchnumbe(time);
        tMedicalDrugsPurchase.setStatus(3L);
        return toAjax(tMedicalDrugsPurchaseService.insertTMedicalDrugsPurchase(tMedicalDrugsPurchase));
    }

    /**
     * 修改药品采购
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:edit')")
    @Log(title = "药品采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMedicalDrugsPurchase tMedicalDrugsPurchase)
    {
        if(tMedicalDrugsPurchase.getVerifier()!=null){
            tMedicalDrugsPurchase.setVerificationtime(new Date());
        }
        if(tMedicalDrugsPurchase.getReviewer()!=null){
            tMedicalDrugsPurchase.setAudittime(new Date());
            TMedicalDrugsStorage storage = new TMedicalDrugsStorage();
            TMedicalDrugsStorage storage1 = tMedicalDrugsStorageService.getDrugsId(tMedicalDrugsPurchase.getDrugsid());
            System.out.println(storage1);
            if(storage1!=null) {
                if (storage1.getDrugsid() == tMedicalDrugsPurchase.getDrugsid()) {
                    storage1.setNo(tMedicalDrugsPurchase.getNo() + storage1.getNo());
                    tMedicalDrugsStorageService.updateTMedicalDrugsStorage(storage1);
                }
            }else{
                storage.setDrugsid(tMedicalDrugsPurchase.getDrugsid());
                storage.setNo(tMedicalDrugsPurchase.getNo());
                tMedicalDrugsStorageService.insertTMedicalDrugsStorage(storage);
            }
        }
        return toAjax(tMedicalDrugsPurchaseService.updateTMedicalDrugsPurchase(tMedicalDrugsPurchase));
    }

    /**
     * 删除药品采购
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:remove')")
    @Log(title = "药品采购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseids}")
    public AjaxResult remove(@PathVariable Long[] purchaseids)
    {
        return toAjax(tMedicalDrugsPurchaseService.deleteTMedicalDrugsPurchaseByPurchaseids(purchaseids));
    }
}
